/**
 * 
 */
package leetCode;

/**
 * @author zhong
 *
 */
public class MinimumDistanceBetweenBSTNodes {
	public class TreeNode {
		int val;
		TreeNode left;
		TreeNode right;

		TreeNode(int x) {
			val = x;
		}
	}

	public int minDiffInBST(TreeNode root) {
		return findDiff(root, new int[2]);
	}

	private int findDiff(TreeNode root, int[] is) {
		int[] left = new int[2];
		int[] right = new int[2];
		is[0] = is[1] = root.val;
		int diff = Integer.MAX_VALUE;
		if (root.left != null) {
			diff = findDiff(root.left, left);// 左递归处理最小差，并且得到左子的最小和最大值
			diff = Math.min(diff, root.val - left[1]);
			is[0] = left[0];
		}
		if (root.right != null) {
			diff = Math.min(diff, findDiff(root.right, right));
			diff = Math.min(diff, right[0] - root.val);
			is[1] = right[1];// 当前这棵树的最大值和最小值
		}
		return diff;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
